<?php
/* 
 * Aclass_sql
 * mysql 类
 */
class Aclass_sqlite{

	function  __construct($data) {
		$this->dbname = $data['name'];
		$this->adbs = Afun_db_link($data);
		$this->cache_name = 'Adb:'.$this->dbname.':';
	}
//PDO
	function exec($sql){
		mysql_query_times();
	//	Afun_debug_mysql($sql);
		//print_r($sql);exit;
	//	return $this->adbs->exec($sql) or die("sql语句错误:$sql");
		return $this->adbs->exec($sql);
	}
	function query($sql){
		mysql_query_times();
	//	Afun_debug_mysql($sql);
		$query = $this->adbs->query($sql) or die("sql语句错误:$sql");
		return $query;
	}
	function lastInsertId(){
		return $this->adbs->lastInsertId();
	}
	function quote($str){
		return $this->adbs->quote($str);
	}
//扩展
	function model($table=""){
		$this->select = 'a.*';
		if(!empty($table)) $this->table = $table;
		$this->columns();
		$this->key_name;
		return $this;
	}
	function ofind($val){
		return Afun_arr_obj($this->find($val));
	}
	function ofindPk($val){
		return Afun_arr_obj($this->findPk($val));
	}
	function ofindAll($val){
		return Afun_arr_obj($this->findAll($val));
	}
	function find($val){
		$sql = $this->unisql($val);
		return $this->query($sql)->fetch();
	}
	function findPk($pk){
		$sql = $this->unisql('`'.$this->key_name.'`=\''.$pk.'\'');
		return $this->query($sql)->fetch();
	}
	function findAll($val){
		$sql = $this->unisql($val);
		return $this->query($sql)->fetchAll();
	}
	function update($val,$con){
		$sql = $this->unisql_update($val,$con);
		return $this->exec($sql);
	}
	function updatePk($val,$pk){
		$sql = $this->unisql_update($val,'`'.$this->key_name.'`=\''.$pk.'\'');
		return $this->exec($sql);
	}
	function updateAll($val,$con=''){
		$sql = $this->unisql_update($val,$con);
		return $this->exec($sql);
	}
	function save($val){
		if(is_array($val)){
			if(empty($val[$this->key_name]) || $val[$this->key_name] == ''){
				$sql = $this->unisql_insert($val);
			}else{
				$sql = $this->updatePk($val,$val[$this->key_name]);
			}
		}elseif(is_object($val)){
			$key_name = $this->key_name;
			if(empty($val->$key_name) || $val->$key_name == ''){
				$sql = $this->unisql_insert($val);
			}else{
				$sql = $this->updatePk($val,$val->$key_name);
			}
		}
		//echo $sql;
		return $this->exec($sql);
	}
	function count($val){
		$sql = $this->unisql($val);
		$sql = preg_replace("/SELECT([0-9a-zA-Z.*_-\s]+)FROM/i",'SELECT count(*) FROM',$sql);
		$data = $this->query($sql)->fetch();
		return $data[0];
	}
	function delete($val){
		$sql = $this->unisql_del($val);
		return $this->exec($sql);
	}
	function deletePk($pk){
		$sql = $this->unisql_del('`'.$this->key_name.'`=\''.$pk.'\'');
		return $this->exec($sql);
	}
	//点击计数
	function click($field,$con){
		return  $this->update("$field = $field+1",$con);
	}
	function clickPk($field,$id){
		return  $this->updatePk("$field = $field+1",$id);
	}
	function unisql_del($val,$data=''){
		if(is_string($val)){
			if(is_array($data)){
				$where = "WHERE '$val' in (".$this->inall($data).")";
			}else{
				$where = "WHERE ".$val;
			}
		}elseif(is_array($val) || is_object($val)){
			foreach($val as $k=>$v){
				if(empty($v) && $v != 0){continue;}
				if(!in_array($k,$this->fields)){continue;}
				if(empty($where)){
					$where = "WHERE `$k` = '$v' ";
				}else{
					$where .= "and `$k` = '$v' ";
				}
			}
		}
		return "DELETE FROM `$this->table` ".$where;
	}
	function unisql_insert($val){
		foreach($val as $k=>$v){
			if(empty($v) && $v != 0){continue;}
			if(!in_array($k,$this->fields)){continue;}
			if(empty($col)){
				$col = '`'.$k.'`';
				$value = '\''.$v.'\'';
			}else{
				$col .= ',`'.$k.'`';
				$value .= ',\''.$v.'\'';
			}
		}
		return "INSERT INTO `$this->table` ($col) VALUES($value)";
	}
	function unisql_update($val,$con){
		if(is_array($val) || is_object($val)){
			foreach($val as $k=>$v){
				if(empty($v) && $v != 0){continue;}
				if(!in_array($k,$this->fields)){continue;}
				if(empty($set)){
					$set = ' SET `'.$k.'`=\''.$v.'\'';
				}else{
					$set .= ',`'.$k.'`=\''.$v.'\'';
				}
			}
		}elseif(is_string($val)){
			$set = ' SET '.$val;
		}
		if(is_string($con)){//字符串条件
			$where = ' WHERE '.$con;
		}elseif(is_array($con) || is_object($con)){
			foreach($con as $k=>$v){
				if(empty($v) && $v != 0){continue;}
				if(!in_array($k,$this->fields)){continue;}
				if(empty($where)){
					$where = ' WHERE `'.$k.'` = \''.$v.'\'';
				}else{
					$where .= ' and `'.$k.'` = \''.$v.'\'';
				}
			}
		}
		return "UPDATE `$this->table`".$set.$where;
	}
	
	function unisql($val){
		$this->model;
		if(is_string($val)){//字符串条件
			$where = ' WHERE '.$val;
			if(empty($val)){$where = '';}
		}
		if(is_array($val)){//数组条件
			foreach($val as $k=>$v){
				if(empty($v) && $v != 0){continue;}
				if(!in_array($k,$this->fields)){continue;}
				if(empty($where)){
					$where = ' WHERE `'.$k.'` = \''.$v.'\'';
				}else{
					$where .= ' and `'.$k.'` = \''.$v.'\'';
				}
			}
		}
		if(is_object($val)){//对象条件
			if($val->select){	$this->select = $val->select;}
			if($val->limit)
				$limit = ' LIMIT '.$val->limit;
			if($val->where){
				$this->where = ' WHERE '.$val->where;
			}
			if($val->join)
				$join = $val->join;
			if($val->group){
				$group = ' GROUP BY '.$val->group;
			}
			if($val->order){
				$by .= ' ORDER BY '.$val->order;
			}
			$where = $join.$this->where.$group.$by.$limit;
		}
		return 'SELECT '.$this->select.' FROM `'.$this->table.'` a '.$where;
	}
	/*	取得当前表的key  返回表的栏目属性  并缓存1小时	*/
	function columns(){
		$cache_name = $this->cache_name.$this->table;
		$data = Afun_cache_get($cache_name);
		if(empty($data)){
			$sql = "SHOW COLUMNS FROM `$this->table`";
			$data = $this->query($sql)->fetchAll();
			foreach($data as $k=>$val){
				if($val['Key'] == 'PRI' && empty($data['key'])){
					$data['key'] = $val['Field'];
				}
				$data['fields'][$k] = $val['Field'];
			}
			Afun_cache_set($cache_name,$data,3600);
		}
		$this->key_name = $data['key'];
		$this->fields = $data['fields'];
		$this->columns = $data;
		return $this;
	}
	
	function inall($arr){
		foreach($arr as $value){
			if(empty($allid))
				$allid = "'".(int)$value."'";
			else
				$allid.= ",'".(int)$value."'";
		}
		return $allid;
	}
	
	function adbs($host='',$user='',$pass='',$name='',$port='',$charset=''){
		if(!$host){	$host = Acan_db_host;}
		if(!$port){	$port = Acan_db_port;}
		if(!$user){	$user = Acan_db_user;}
		if(!$pass){	$pass = Acan_db_pass;}
		if(!$name){	$name = Acan_db_name;}
		if(!$charset){	$charset = Acan_db_charset;}
		return Afun_db_link($host,$user,$pass,$name,$charset);
	}
	//Asql update(数据,表名,条件)
	function update2($arr,$table,$where){
		foreach($arr as $key=>$val){
			if(empty($sql_save[$i])){
				$sql_save = "`$key`='".$val."'";
			}else{
				$sql_save .= ",`$key`='".$val."'";
			}
		}
		$sql = "UPDATE `".Acan_db_pre.$table."` SET ".$sql_save." WHERE ".$where;
		if($this->exec($sql))
			return true;
		else
			return false;
	}
	//Asql create(数据,表名,条件)
	function create($arr,$table){
		foreach($arr as $col=>$val){
			if(empty($cola)){
				$cola = '`'.$col.'`';
				$colb = "'".$val."'";
			}else{
				$cola .= ',`'.$col.'`';
				$colb .= ",'".$val."'";
			}
		}
		$cgstr="INSERT INTO `".Acan_db_pre.$table."` ($cola) VALUES ($colb)";
		if($this->query($cgstr))
			return true;
		else
			return false;
	}

	//Asql del(数据,表名,条件)
	function Asql_delKey($key,$table){
		$sql = "DELETE FROM `".Acan_db_pre.$table."` WHERE id = '".$age['del_id']."'";
		if($this->exec($sql))
			return true;
		else
			return false;
	}



}
?>
